#include <iostream>

using namespace std;

int getMaxChildValue(int bufValue[],int length)
{
    int resultSum = 0; 
    int tempSum = 0; 
    int maxValue = 0; 

    for (int i = 0; i < length; ++i)
    {
        if (tempSum <= 0)
            tempSum = bufValue[i];
        else
            tempSum += bufValue[i]; 
        if (tempSum > resultSum)
            resultSum = tempSum;
        if (bufValue[i] > maxValue)
            maxValue = resultSum;
    }
    if (resultSum < 0)
        return maxValue;
    else
        return resultSum;
}
//test
int main(int argc, char *argv[])
{
    int buf[17] = { -32,-10,33,-23,32,-12,41,-12,1,3,5,-98,70,-21,10,-9,61};
    cout <<"sum="<< getMaxChildValue(buf, sizeof(buf)/sizeof(int))<<endl;
    getchar();
    return 0;
}
